<?php
require_once "../load.php";
error_reporting(0);
$action = $_POST["action"];
unset($_POST["action"]);
if (get_magic_quotes_gpc()) {
    $_POST = Owl_Format::stripslashes_deep($_POST);
}

switch ($action) {
    case 'user_login' :
        $user = Owl_User::get_instance();
        $password = trim($_POST["password"]);
        $lang = ($_POST["lang"] == "default") ? false : $_POST["lang"];
        $cookie = ($_POST["cookie"] == "on");
        if ($user->log_in_user($password, $lang, $cookie)) {
            echo "1";
        }
        else {
            echo '<div class="ajax-error">' . __("Password is wrong")  . '</div>';
        }
        break;
    case 'edit_password' :
        $user = Owl_User::get_instance();
        if (!$user->is_logged_in()) {
            die();
        }
        header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
        header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
        header("Cache-Control: no-cache, must-revalidate" );
        header("Pragma: no-cache" );
        header("Content-type: text/x-json");

        $old_password = trim($_POST["old_password"]);
        $new_password = trim($_POST["new_password"]);
        $password_retype = trim($_POST["password_retype"]);
        
        //Check new password and password retype
        if ($new_password != $password_retype) {
            $data = array(
                "code" => 0,
                "message" => '<div class="ajax-error">' . __("New password and password-retype do not match")  . '</div>'
            );
        }
        else {
            $owl_personal = new Owl_Personal();
            if ($owl_personal->change_password($old_password, $new_password)) {
                $data = array(
                    "code" => 1,
                    "message" => '<div class="ajax-success">' . __("Change password successful!")  . '</div>'
                );
            }
            else {
                $data = array(
                    "code" => 1,
                    "message" => '<div class="ajax-error">' . $owl_personal->get_error()  . '</div>'
                );
            }
        }

        echo Owl_Functions::json_encode($data);
        break;
    case 'change_lang' :
        $_SESSION['user']['lang'] = $_POST['lang'];
        echo "1";
        break;
    default:
        die();
}